home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / MacsBug OSA / source code / UMacsBugApplication.cp < prev    next >
Encoding:
Text File  |  1997-06-27  |  8.3 KB  |  317 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UMacsBugApplication.cp
  3. // Copyright © 1991-96 by Apple Computer, Inc. All rights reserved. 
  4. //----------------------------------------------------------------------------------------
  5.  
  6. /*
  7.     Change History:
  8.          6/27/97    TWB        QTML headers: Include Sound.h to get SysBeep. 
  9.         ----- R11 -----
  10.         06/26/96    mdr        Install release marker.  Scope many globals.  Change TRUE/FALSE/NULL.
  11.         06/11/96    gjc        Add global scoping for toolbox calls
  12.         06/01/96    gjc        [1327013] kSignature =  SS02 - It was using 
  13.                             the default value (SS01), which conflict with "Nothing".
  14.                             See the .r file for more details.
  15.                             Added Change history. 
  16.         ----- R10 -----
  17. */
  18.  
  19. #ifndef __UMACSBUGAPPLICATION__
  20. #include "UMacsBugApplication.h"
  21. #endif
  22.  
  23. #ifndef __MACSBUG_R_H__
  24. #include "MacsBug.r.h"
  25. #endif
  26.  
  27. // MacApp
  28.  
  29. #ifndef __UMENUMGR__
  30. #include "UMenuMgr.h"
  31. #endif
  32.  
  33. // Toolbox
  34.  
  35. #ifndef __SOUND__
  36. #include <Sound.h>
  37. #endif
  38.  
  39.  
  40. //========================================================================================
  41. // GLOBAL Functions
  42. //========================================================================================
  43.  
  44. void HeapCheck(TAppleEvent* message, TAppleEvent* reply);
  45. void HeapTotal(TAppleEvent* message, TAppleEvent* reply);
  46. void HeapDisplay(TAppleEvent* message, TAppleEvent* reply);
  47. void SpecifyHeap(TAppleEvent* message, CStr255& command);
  48. void DoMacsBugCommand(const CStr255& commandString, TAppleEvent* reply);
  49. void DumpAppleEvent(const AEDescList* message);
  50.  
  51. //========================================================================================
  52. // CLASS TMacsBugApplication
  53. //========================================================================================
  54. #undef Inherited
  55. #define Inherited TApplication
  56.  
  57. #pragma segment AInit
  58. MA_DEFINE_CLASS_M1(TMacsBugApplication, Inherited);
  59.  
  60. //----------------------------------------------------------------------------------------
  61. // TMacsBugApplication constructor
  62. //----------------------------------------------------------------------------------------
  63. #pragma segment AOpen
  64.  
  65. TMacsBugApplication::TMacsBugApplication()
  66. {
  67. }
  68.  
  69. //----------------------------------------------------------------------------------------
  70. // TMacsBugApplication destructor
  71. //----------------------------------------------------------------------------------------
  72. #pragma segment MADestructorRes
  73.  
  74. TMacsBugApplication::~TMacsBugApplication()
  75. {
  76. }
  77.  
  78. //----------------------------------------------------------------------------------------
  79. // TMacsBugApplication::IMacsBugApplication: 
  80. //----------------------------------------------------------------------------------------
  81. #pragma segment AInit
  82.  
  83. void TMacsBugApplication::IMacsBugApplication()
  84. {
  85.     IApplication(kFileType, kSignature);
  86.     
  87.     fLaunchWithNewDocument = false;
  88. }
  89.  
  90. //----------------------------------------------------------------------------------------
  91. // TMacsBugApplication::DoScriptCommand
  92. //----------------------------------------------------------------------------------------
  93.  
  94. void TMacsBugApplication::DoScriptCommand(CommandNumber aCommand, TAppleEvent* message,
  95.                                           TAppleEvent* reply)
  96. {
  97.     switch (aCommand)
  98.     {
  99.         case cHeapCheck:
  100.             HeapCheck(message, reply);
  101.             break;
  102.  
  103.         case cHeapZones:
  104.             DoMacsBugCommand("\phz", reply);
  105.             break;
  106.  
  107.         case cHeapTotal:
  108.             HeapTotal(message, reply);
  109.             break;
  110.  
  111.         case cHeapDisplay:
  112.             HeapDisplay(message, reply);
  113.             break;
  114.  
  115.         default:
  116.             Inherited::DoScriptCommand(aCommand, message, reply);
  117.             break;
  118.     }
  119. }
  120.  
  121. //----------------------------------------------------------------------------------------
  122. // HeapCheck
  123. //----------------------------------------------------------------------------------------
  124.  
  125. void HeapCheck(TAppleEvent* message, TAppleEvent* reply)
  126. {
  127.     CStr255 command;
  128.     command.Empty();
  129.     
  130.     SpecifyHeap(message, command);
  131.  
  132.     command += "hc";
  133.  
  134.     DoMacsBugCommand(command, reply);
  135. }
  136.  
  137. //----------------------------------------------------------------------------------------
  138. // HeapTotal
  139. //----------------------------------------------------------------------------------------
  140.  
  141. void HeapTotal(TAppleEvent* message, TAppleEvent* reply)
  142. {
  143.     CStr255 command;
  144.     command.Empty();
  145.     
  146.     SpecifyHeap(message, command);
  147.  
  148.     command += "ht";
  149.  
  150.     DoMacsBugCommand(command, reply);
  151. }
  152.  
  153. //----------------------------------------------------------------------------------------
  154. // HeapDisplay
  155. //----------------------------------------------------------------------------------------
  156.  
  157. void HeapDisplay(TAppleEvent* message, TAppleEvent* reply)
  158. {
  159.     CStr255 command;
  160.     command.Empty();
  161.     
  162.     SpecifyHeap(message, command);
  163.  
  164.     command += "hd";
  165.  
  166.     DoMacsBugCommand(command, reply);
  167. }
  168.  
  169. //----------------------------------------------------------------------------------------
  170. // SpecifyHeap
  171. //----------------------------------------------------------------------------------------
  172.  
  173. void SpecifyHeap(TAppleEvent* message, CStr255& command)
  174. {
  175. #if qDebug
  176.     static Boolean pDumpMessage;
  177.     if (pDumpMessage)
  178.         DumpAppleEvent(message);
  179. #endif
  180.  
  181.     if (message->HasParameter(keyAEMacsBugZone))
  182.     {
  183.         DescType zoneParamType = message->ParameterType(keyAEMacsBugZone);
  184.         if (zoneParamType == typeEnumerated)
  185.         {
  186.             DescType zoneParam = message->ReadEnum(keyAEMacsBugZone);
  187.             if (zoneParam == kAEMacsBugZoneSystem)
  188.                 command += "hx SysZone^;";
  189.             else if (zoneParam == kAEMacsBugZoneApplication)
  190.                 command += "hx TheZone^;";
  191.             else if (zoneParam == kAEMacsBugZoneUser)
  192.             {
  193.             }
  194.         }
  195.         else
  196.         {
  197.             CStr255 zone;
  198.             message->ReadString(keyAEMacsBugZone, zone);
  199.             command = "hx #";
  200.             command += zone;
  201.             command += ';';
  202.         }
  203.     }
  204. }
  205.  
  206. //----------------------------------------------------------------------------------------
  207. // DoMacsBugCommand
  208. //----------------------------------------------------------------------------------------
  209.  
  210. void DoMacsBugCommand(const CStr255& commandString, TAppleEvent* reply)
  211. {
  212.     TFile* logFile = NULL;
  213.     Handle responseHandle = NULL;
  214.  
  215.     TRY
  216.     {
  217.         CFSSpec logSpec;
  218.         logSpec.vRefNum = 0;
  219.         logSpec.parID = 0;
  220.         CStr63("\pMacsBug.log.temp").CopyTo(logSpec.name);
  221.  
  222.         logFile = NewFile('TEXT', 'MPS ', kUsesDataFork, noResourceFork, kDataOpen, !kRsrcOpen);
  223.         logFile->Specify(logSpec);
  224.         CStr255 logPath;
  225.         FailOSErr(logFile->GetPathName(logPath));
  226.         logPath += CStr63(logSpec.name);
  227.  
  228.         CStr255 macsBugString = "\p;set suspendprompt;log \"";
  229.         macsBugString += logPath;
  230.         macsBugString += CStr15("\p\";");
  231.         macsBugString += commandString + ";log;g";
  232.         DebugStr(macsBugString);
  233.  
  234.         FailOSErr(logFile->OpenFile());
  235.         Size responseLength;
  236.         FailOSErr(logFile->GetDataLength(responseLength));
  237.         FailOSErr(logFile->SetDataMark(0, fsFromStart));
  238.  
  239.         responseHandle = NewPermHandleClear(responseLength);
  240.         FailNIL(responseHandle);
  241.  
  242.         HLock(responseHandle);
  243.  
  244.         FailOSErr(logFile->ReadData(*responseHandle, responseLength));
  245.         FailOSErr(logFile->CloseFile());
  246.         
  247.         responseLength -= 14;
  248.  
  249.         if (responseLength > 0)
  250.             reply->WriteParameterPtr(keyDirectObject, typeChar, *responseHandle, responseLength);
  251.  
  252.         HUnlock(responseHandle);
  253.  
  254.         responseHandle = DisposeIfHandle(responseHandle);
  255.  
  256.         FailOSErr(logFile->DeleteFile());
  257.         delete logFile; logFile = NULL;
  258.     }
  259.     CATCH_ALL
  260.     {
  261.         responseHandle = DisposeIfHandle(responseHandle);
  262.         delete logFile; logFile = NULL;
  263.     }
  264.     ENDTRY
  265. }
  266.  
  267. //----------------------------------------------------------------------------------------
  268. // DumpAppleEvent
  269. //----------------------------------------------------------------------------------------
  270.  
  271. void DumpAppleEvent(const AEDescList* message)
  272. {
  273.     fprintf(stderr, "Dumping message\n");
  274.  
  275.     long count;
  276.     FailOSErr(AECountItems(message, &count));
  277.  
  278.     fprintf(stderr, "count = %d\n", count);
  279.  
  280.     //fprintf(stderr, "<pre><tt>\n");
  281.  
  282.     for (long index = 1; index <= count; index++)
  283.     {
  284.         fprintf(stderr, "%3d)", index);
  285.  
  286.         AEKeyword key;
  287.         DescType typeCode;
  288.         Size actualSize;
  289.         OSErr err = AEGetNthPtr(message, index, typeWildCard, &key, &typeCode, NULL, 0, &actualSize);
  290.         if (!err)
  291.         {
  292.             fprintf(stderr, "  %.4s %.4s %6d", (char*)&key, (char*)&typeCode, actualSize);
  293.  
  294.             char text[256];
  295.             err = AEGetNthPtr(message, index, typeChar, &key, &typeCode, text, 255, &actualSize);
  296.             if (!err)
  297.             {
  298.                 text[actualSize] = 0;
  299.                 fprintf(stderr, "  %s", text);
  300.             }
  301.         }
  302.  
  303.         if (err != noErr)
  304.             fprintf(stderr, "  error %d", err);
  305.  
  306.         fprintf(stderr, "\n");
  307.     }
  308.  
  309.     //fprintf(stderr, "</tt></pre>\n");
  310.     //fprintf(stderr, "<br>\n");
  311. }
  312.  
  313. //----------------------------------------------------------------------------------------
  314. // End of UMacsBugApplication.cp
  315.  
  316. #pragma segment Inline
  317.